package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.vo.MemberVO;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    OrderMapper orderMapper;


    public MemberVO findByTelephone(String tel) {

        MemberVO member = memberMapper.findByTelephone(tel);
        return member;
    }


    public void add(MemberVO member) {

        String password = member.getPassword();
        if (password != null) {
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }

        memberMapper.add(member);
    }

    @Override
    public void addMemberAndOrder(MemberVO member, int[] setMealids, String orderDate) throws Exception {
        String password = member.getPassword();
        if (password != null) {
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberMapper.add(member);

        if (setMealids!=null&&setMealids.length>1){
            for (int setMealid : setMealids) {
                Order order1 = new Order();
                order1.setMemberId(member.getId());
                order1.setOrderDate(DateUtils.parseString2Date(orderDate));
                order1.setOrderType(Order.ORDERTYPE_WEIXIN);
                order1.setOrderStatus(Order.ORDERSTATUS_NO);
                order1.setSetmealId(setMealid);
                orderMapper.add(order1);
            }
        }
        Order order2 = new Order();
        order2.setMemberId(member.getId());
        order2.setOrderDate(DateUtils.parseString2Date(orderDate));
        order2.setOrderType(Order.ORDERTYPE_TELEPHONE);
        order2.setOrderStatus(Order.ORDERSTATUS_NO);
        if (setMealids!=null&&setMealids.length>0){
            order2.setSetmealId(setMealids[0]);
        }
        orderMapper.add(order2);

        orderMapper.updateReservations(orderDate);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {

//        List<Integer> memberCount = new ArrayList<>();
//
//        for (String month : months) {
//            month=month+".31";
//            Integer count = memberMapper.findMemberCountBeforeDate(month);
//            memberCount.add(count);
//        }
//        return memberCount;
//    }

        List<Integer> list = memberMapper.findMemberCountBeforeDate_2(months);
        return list;
    }

    /**
     * 会员信息分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //基于MyBatis的分页助手
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        Page<MemberVO> page = memberMapper.selectByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据会员id删除会员
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) throws RuntimeException {
        memberMapper.deleteById(id);
    }

    /**
     * 删除选中的会员
     *
     * @param memberVOs
     */
    @Override
    public void deleteSelected(MemberVO[] memberVOs) throws RuntimeException {
        if (memberVOs != null && memberVOs.length > 0) {
            //数组不为空,遍历该数组
            for (MemberVO memberVO : memberVOs) {
                //调用mapper的方法,根据每个对象的id删除
                memberMapper.deleteById(memberVO.getId());
            }
        }
    }

    /**
     * 根据id查询会员信息
     * @param id
     * @return
     */
    @Override
    public MemberVO findById(Integer id) {
        return memberMapper.findById(id);
    }

    /**
     * 编辑会员信息
     * @param memberVO
     */
    @Override
    public void edit(MemberVO memberVO) {
        memberMapper.edit(memberVO);
    }

    /**
     * 获取会员报告
     * @return
     */
    @Override
    public List<Map<String, Object>> getMemberReportData() {
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> list = memberMapper.getMemberReport();
        return list;
    }
}
